package j2024.j202407;

import java.util.ArrayDeque;import java.util.Queue;

public class j0710 {
    //BFS
    //733
    int[] sx = {0,0,-1,1};
    int[] sy = {1,-1,0,0};
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        int prev = image[sr][sc];
        if(prev==color){
            return image;
        }
        int c = image.length,r = image[0].length;
        Queue<int[]> q = new ArrayDeque<>();
        q.add(new int[]{sr,sc});
        while(!q.isEmpty()){
            int[] tmp = q.poll();
            int a = tmp[0],b = tmp[1];
            image[a][b] = color;
            //上下左右四个方向
            for (int i = 0; i < 4; i++) {
                int x = a+sx[i];
                int y = b+sy[i];
                //符合条件就把下标放进队列中
                if(x>=0 && x<c && y>=0 && y<r && image[x][y] == prev){
                    q.add(new int[]{x,y});
                }
            }
        }
        return image;
    }
}
